package com.sunyard.manage.util;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Enumeration;


/**
 * @author k3887
 * @Description 文件解压缩工具类
 * @date 2018-11-22 下午4:07:19
 */

public class FileOperateUtil {
    /**
     * Description
     */
    private static final Logger logger = LoggerFactory.getLogger(FileOperateUtil.class);

    /**
     * Description
     */
    public static void setLogPath(String logPath) {
        FileOperateUtil.logPath = logPath;
    }

    /**
     * Description
     */
    private static String logPath = "";

    /**
     * Description
     */
    @SuppressWarnings("unchecked")
    public static void unZipFiles(String zipFilePath, String fileSavePath) throws IOException {
        FileOperateUtil fileOperateUtil = new FileOperateUtil();

        try {
            (new File(fileSavePath)).mkdirs();
            File f = new File(zipFilePath);
            if ((!f.exists()) && (f.length() <= 0)) {
                throw new RuntimeException("not find" + zipFilePath + "!");
            }
            ZipFile zipFile = new ZipFile(f, "GBK");
            String gbkPath, strtemp;

            Enumeration<ZipEntry> e = zipFile.getEntries();
            while (e.hasMoreElements()) {
                org.apache.tools.zip.ZipEntry zipEntry = e.nextElement();
                gbkPath = zipEntry.getName();
                strtemp = fileSavePath + File.separator + gbkPath;
                if (zipEntry.isDirectory()) {
                    File dir = new File(strtemp);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    continue;
                } else {
                    InputStream is = zipFile.getInputStream(zipEntry);
                    BufferedInputStream bis = new BufferedInputStream(is);
                    String strsubdir = gbkPath;
                    for (int i = 0; i < strsubdir.length(); i++) {
                        if ("/".equalsIgnoreCase(strsubdir.substring(i, i + 1))) {
                            String temp = fileSavePath + File.separator + strsubdir.substring(0, i);
                            File subdir = new File(temp);
                            if (!subdir.exists()) {
                                subdir.mkdirs();
                            }
                        }
                    }
                    FileOutputStream fos = new FileOutputStream(strtemp);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    int len;
                    byte[] buff = new byte[5120];
                    while ((len = bis.read(buff)) != -1) {
                        bos.write(buff, 0, len);
                    }
                    bos.close();
                    fos.close();
                }
            }
            logger.info("文件解压成功。。。。");
            zipFile.close();
        } catch (Exception e) {
            logger.error("解压文件异常：", e);
            fileOperateUtil.WriteStringToFile(logPath, "extract file error:" + zipFilePath);

        }
    }

    /**
     * Description
     */
    public void WriteStringToFile(String filePath, String content) throws IOException {
        try {
            FileWriter fw = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content + "\r\n");
            bw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Description
     */
    public static void main(String[] args) throws IOException {
        unZipFiles("C:\\测试\\20181016.zip", "C:\\测试\\20181016");
    }

}